package eip.chapter6.messagefilter.mule;

import java.io.StringReader;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.JiBXException;
import org.mule.transformers.AbstractTransformer;
import org.mule.umo.transformer.TransformerException;

import eip.chapter6.messagefilter.DeclarationMessage;

/**
 * Transforms an incoming XML file to a declarationMessage
 * @author nl24167
 *
 */
public class XMLToDeclarationMessageTransformer extends AbstractTransformer {

	private static final long serialVersionUID = 4453281126202478611L;

	/**
	 * Do the transformation using JiBX.
	 */
	@Override
	protected Object doTransform(Object src, String encoding)
			throws TransformerException {
		String xmlInput = (String) src;
		Object result = null;
	    try {
			IBindingFactory bfact = BindingDirectory.getFactory(DeclarationMessage.class);
			result = (DeclarationMessage) bfact.createUnmarshallingContext().
						unmarshalDocument(new StringReader(xmlInput),null);
		} catch (JiBXException e) {
			throw new TransformerException(this,e);
		}
		return result;
	}

}
